<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>insertionSort</title>
  </head>

  <body>
    <script>
      function swap(array, a, b) {
        [array[a], array[b]] = [array[b], array[a]];
      }
      function insertionSort(array) {
        let length = array.length;
        for (let i = 1; i < length; i++) {
          for (let j = i; j > 0; j--) {
            if (array[j] < array[j - 1]) {
              swap(array, j, j - 1);
            }
          }
        }
        return array;
      }
      let arr = [123, 14, 521, 8, 31, 943, 19, 56, 241];
      console.log(insertionSort(arr));
    </script>
  </body>
</html>
